<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">车牌号码：</span>
      <el-input v-model="params.carNumber" clearable placeholder="请输入内容" class="search-main" />
      <span class="search-label">车主姓名：</span>
      <el-input v-model="params.personName" clearable placeholder="请输入内容" class="search-main" />
      <span class="search-label">状态：</span>
      <el-select v-model="params.cardStatus" placeholder="请选择">
        <el-option v-for="item in options" :key="item.value" :value="item.value" :label="item.label" />
      </el-select>
      <el-button type="primary" class="search-btn" @click="search">查询</el-button>
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="$router.push('/cardAdd')">添加月卡</el-button>
      <el-button @click="delList">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="CarData" @select="select">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="index" type="index" label="序号" />
        <el-table-column prop="personName" label="车主名称" />
        <el-table-column prop="phoneNumber" label="联系方式" />
        <el-table-column prop="carNumber" label="车牌号码" />
        <el-table-column prop="carBrand" label="车辆品牌" />
        <el-table-column prop="totalEffectiveDate" label="剩余有效天数" />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="{ row }">
            <el-button size="mini" type="text">续费</el-button>
            <el-button size="mini" type="text">查看</el-button>
            <el-button size="mini" type="text" @click="$router.push(`/cardAdd?id=${row.id}`)">编辑</el-button>
            <el-button size="mini" type="text" @click="del(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        :current-page.sync="params.page"
        layout="total, prev, pager, next"
        :total="total"
        :page-size="5"
        @current-change="getCarAreaList"
      />
    </div>
    <!-- 添加楼宇 -->
    <el-dialog title="添加楼宇" width="580px">
      <!-- 表单接口 -->
      <div class="form-container">
        <!-- <el-form ref="addForm" :model="addForm" :rules="addFormRules">
          <el-form-item label="楼宇名称" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="楼宇层数" prop="floors">
            <el-input v-model="addForm.floors" />
          </el-form-item>
          <el-form-item label="在管面积" prop="area">
            <el-input v-model="addForm.area" />
          </el-form-item>
          <el-form-item label="物业费" prop="propertyFeePrice">
            <el-input v-model="addForm.propertyFeePrice" />
          </el-form-item>
        </el-form> -->
      </div>
      <template #footer>
        <el-button size="mini">取 消</el-button>
        <el-button size="mini" type="primary">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getCarList, deleteCar } from '@/api/car/carCard'
export default {
  name: 'CarArea',
  data() {
    return {
      addForm: {
      }, // 搜索数据
      addFormRules: {

      }, // 表单验证
      params: {
        page: 1,
        pageSize: 5,
        carNumber: '',
        personName: '',
        cardStatus: ''
      }, // 请求参数
      options: [
        { label: '全部', value: '' },
        { label: '可用', value: 0 },
        { label: '已过期', value: 1 }
      ],
      total: null, // 总条数
      CarData: [], // 表格数据
      selectArr: []
    }
  },
  created() {
    this.getCarAreaList()
  },
  methods: {
    // 获取表格数据
    async getCarAreaList() {
      const { total, rows } = await getCarList(this.params)
      this.total = total
      this.CarData = rows
      console.log(this.CarData)
    },
    // 查询
    search() {
      this.params.page = 1
      this.getCarAreaList()
    },
    // 删除
    async del(row) {
      await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteCar(row.id)
        this.getCarAreaList()
        return this.$message({
          type: 'success',
          message: '删除成功!'
        })
      })
    },
    // 批量删除
    async delList() {
      if (this.selectArr.length === 0) { return this.$message.error('请选择要删除的数据') }
      await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // console.log(this.selectArr)
        await deleteCar(...this.selectArr)
        this.getCarAreaList()
        return this.$message({
          type: 'success',
          message: '删除成功!'
        })
      })
    },
    select(value) {
      // console.log(value)
      value.forEach(item => {
        if (!this.selectArr.includes(item.id)) { this.selectArr.push(item.id) }
      })
      // console.log(this.selectArr)
    }
  }
}
</script>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-main {
    width: 220px;
    margin-right: 10px;
  }

  .search-btn {
    margin-left: 20px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>
